<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>菜单管理 - 红高粱</title>
<%@ include file="/WEB-INF/jsp/common/res.jsp"%>
<link href="<c:url value="/scripts/jquery/validate/css/validate.css"/>" rel="stylesheet"/>
<script src="<c:url value="/scripts/jquery/validate/jquery.validate.min.js"/>"></script> 
<script charset="utf-8" src="<c:url value="/widgets/kindeditor/kindeditor.js"/>"></script>
<script charset="utf-8" src="<c:url value="/widgets/kindeditor/lang/zh_CN.js"/>"></script>
<script type="text/javascript" src="<c:url value="/scripts/product/editProduct.js"/>"></script>
<style type="text/css">
#_category{background-color: #0087d8;} 
.text{width:300px;height:20px;color:#666666;}
.menu{text-align:center;padding:4px;padding-right:5px;width: 300px;}
#img{cursor: pointer;}
</style>
</head>
<body>
	<%@ include file="/WEB-INF/jsp/common/header.jsp"%>
	<div class="main_title">
		<div class="main_title2"><span>当前位置：店铺管理 &gt; 编辑菜品</span></div>
	</div>
	<div class="main_content">
			<div class="main_menu">
				<ul>
					<li>
						<a href="<c:url value="/categorym/query"/>">店铺信息</a>
					</li>
					<li><a href="<c:url value="/categorym/add"/>">添加店铺</a></li>
					<li><a href="<c:url value="/categorym/edit/${pid}"/>">修改店铺</a></li>
					<li class="choice" style="color:#FFFFFF;"><a href="<c:url value="/categorym/editSubCategory/${pid}"/>">管理菜品</a></li>
				</ul>
			</div>
			<div class="main_list">
				<div style="margin:30px 50px">
					<form action="<c:url value="/categorym/submitEditSubCategory"/>" method="post" id="editSubCategoryForm">
						<c:if test="${fn:length(foods) eq 0 }"><span id="tip" style="color:red;text-align: center;">该店铺还没有菜单.</span></c:if>
						<c:forEach var="p" items="${foods}" varStatus="var">
							<p id="p${p.id }">
								<input type="text" name="old" id="${p.id }" class="text" value="${p.name}" readonly="readonly" onblur="javascript:changeStatus('${p.id }');"/>&nbsp;<a href='javascript:edit("${p.id }");'>编辑</a>&nbsp;<a href='javascript:del("p${p.id }");'>删除</a>
							</p>
						</c:forEach>
						<p id="beforeInsert">
							<input type="hidden" name="customerId" id="customerId" value="${customerId }"/>
							<input type="hidden" name="return" value="${returnUrl}"/>
							<input type="hidden" name="pid" value="${pid}"/>
							<input type="hidden" name="oldParams" />
							<input type="button" id="addCategory" value="添加" class="btn" />
							<input type="submit" value="保存" class="btn" />
						</p>
					</form>
				</div>
			</div>
	</div>
	<script type="text/javascript">
		Array.prototype.contains = function(obj) { 
			var i = this.length; 
			while (i--) { 
				if (this[i] === obj) { 
					return true; 
				} 
			} 
			return false; 
		} 
	
		var genId = 0;
		$(function(){
        	$('#addCategory').click(function(){
        		genId++;
			 	var inserttext="<p id='tmp"+genId+"'><input type='text' name='newest' class='text' />&nbsp;<a href='javascript:edit(\"tmp"+genId+"\");'>编辑</a>&nbsp;<a href='javascript:del(\"tmp"+genId+"\");'>删除</a></p>";
			 	$('#beforeInsert').before(inserttext); 
			 	if($('#tip').size()!=0) {
			 		if($('#tip').attr('style').indexOf('display:none')==-1) {
				 		$('#tip').attr('style', 'display:none');
				 	}
			 	}
        	});
        	
        	$("#editSubCategoryForm").submit(function(){  
        		var stringbuilder="";
        		var valid = true;
        		var allNames = new Array();
        		var dup=false;
        		$.each($('input[name=old]'), function(i,val){  
        			if(val.value.indexOf("____")!=-1) {
        				alert(val.value+' 不是正确的菜单名');
            			valid = false;
        			} 
        			if(val.value.indexOf(";")!=-1) {
        				alert(val.value+' 不是正确的菜单名');
            			valid = false;
        			}
        			if(val.value.replace('/(^\s*)|(\s*$)/g', "")=='') {
        				alert('菜单名不能为空');
            			valid = false;
        			}
        			if(allNames.contains(val.value)) {
            			valid = false;
            			dup = true;
        			} else {
        				allNames.push(val.value);
        			}
       		  	}); 
        		$.each($('input[name=newest]'), function(i,val){  
        			if(val.value.indexOf("____")!=-1) {
        				alert(val.value+' 不是正确的菜单名');
            			valid = false;
        			} 
        			if(val.value.indexOf(";")!=-1) {
        				alert(val.value+' 不是正确的菜单名');
            			valid = false;
        			}
        			if(val.value.replace('/(^\s*)|(\s*$)/g', "")=='') {
        				alert('菜单名不能为空');
            			valid = false;
        			}
        			if(allNames.contains(val.value)) {
            			valid = false;
            			dup = true;
        			} else {
        				allNames.push(val.value);
        			}
       		  	}); 
        		if(dup)
        			alert('不能有重复的菜单名');
        		if(!valid)
        			return false;
        			
        		$.each($('input[name=old]'), function(i,val){  
        			stringbuilder+=(val.id+"____"+val.value+";");
       		  	});   
        		
        		$("input[name=oldParams]").attr("value", stringbuilder);
        		return true;
        	}); 
		});
		
		function edit(id){
			$('#'+id).removeAttr("readonly");
			$('#'+id).focus();
		}
		
		function changeStatus(id){
			$('#'+id).attr("readonly", "readonly");
		}
		
		function del(id){
			if(confirm("删除该分类将删除此分类及其子分类下的所有商品，是否删除？")){
				$('#'+id).remove();
			}
		}
    </script>
</body>
</html>